import { anchorIsValid as u } from '../doc/anchors.js';
import {
  isPair as g,
  isAlias as c,
  isNode as S,
  isScalar as a,
  isCollection as y,
} from '../nodes/identity.js';
import { stringifyComment as h } from './stringifyComment.js';
import { stringifyString as p } from './stringifyString.js';
function P(e, t) {
  const o = Object.assign(
    {
      blockQuote: !0,
      commentString: h,
      defaultKeyType: null,
      defaultStringType: 'PLAIN',
      directives: null,
      doubleQuotedAsJSON: !1,
      doubleQuotedMinMultiLineLength: 40,
      falseStr: 'false',
      flowCollectionPadding: !0,
      indentSeq: !0,
      lineWidth: 80,
      minContentWidth: 20,
      nullStr: 'null',
      simpleKeys: !1,
      singleQuote: null,
      trueStr: 'true',
      verifyAliasOrder: !0,
    },
    e.schema.toStringOptions,
    t,
  );
  let r;
  switch (o.collectionStyle) {
    case 'block':
      r = !1;
      break;
    case 'flow':
      r = !0;
      break;
    default:
      r = null;
  }
  return {
    anchors: /* @__PURE__ */ new Set(),
    doc: e,
    flowCollectionPadding: o.flowCollectionPadding ? ' ' : '',
    indent: '',
    indentStep: typeof o.indent == 'number' ? ' '.repeat(o.indent) : '  ',
    inFlow: r,
    options: o,
  };
}
function v(e, t) {
  var s;
  if (t.tag) {
    const n = e.filter(i => i.tag === t.tag);
    if (n.length > 0) return n.find(i => i.format === t.format) ?? n[0];
  }
  let o, r;
  if (a(t)) {
    r = t.value;
    const n = e.filter(i => {
      var l;
      return (l = i.identify) == null ? void 0 : l.call(i, r);
    });
    o = n.find(i => i.format === t.format) ?? n.find(i => !i.format);
  } else (r = t), (o = e.find(n => n.nodeClass && r instanceof n.nodeClass));
  if (!o) {
    const n =
      ((s = r == null ? void 0 : r.constructor) == null ? void 0 : s.name) ??
      typeof r;
    throw new Error(`Tag not resolved for ${n} value`);
  }
  return o;
}
function w(e, t, { anchors: o, doc: r }) {
  if (!r.directives) return '';
  const s = [],
    n = (a(e) || y(e)) && e.anchor;
  n && u(n) && (o.add(n), s.push(`&${n}`));
  const i = e.tag ? e.tag : t.default ? null : t.tag;
  return i && s.push(r.directives.tagString(i)), s.join(' ');
}
function T(e, t, o, r) {
  var f;
  if (g(e)) return e.toString(t, o, r);
  if (c(e)) {
    if (t.doc.directives) return e.toString(t);
    if ((f = t.resolvedAliases) != null && f.has(e))
      throw new TypeError(
        'Cannot stringify circular structure without alias nodes',
      );
    t.resolvedAliases
      ? t.resolvedAliases.add(e)
      : (t.resolvedAliases = /* @__PURE__ */ new Set([e])),
      (e = e.resolve(t.doc));
  }
  let s;
  const n = S(e) ? e : t.doc.createNode(e, { onTagObj: d => (s = d) });
  s || (s = v(t.doc.schema.tags, n));
  const i = w(n, s, t);
  i.length > 0 && (t.indentAtStart = (t.indentAtStart ?? 0) + i.length + 1);
  const l =
    typeof s.stringify == 'function'
      ? s.stringify(n, t, o, r)
      : a(n)
        ? p(n, t, o, r)
        : n.toString(t, o, r);
  return i
    ? a(n) || l[0] === '{' || l[0] === '['
      ? `${i} ${l}`
      : `${i}
${t.indent}${l}`
    : l;
}
export { P as createStringifyContext, T as stringify };
